package com.zw.mes.module.system.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.common.ValidResult;
import com.zw.mes.entity.sys.Menu;
import com.zw.mes.module.system.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 权限接口
 *
 * 权限：
 * system:menu:view 查看
 * system:menu:edit 编辑
 *
 * @Author: China.sgl
 * @Date: 2019/11/28 13:48
 */

@Controller
@RequestMapping("/sys/menu")
@SuppressWarnings("all")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @GetMapping
    @RequiresPermissions({"system:menu:view"})
    public String page() {
        return "system/menu/page";
    }

    /**
     * 系统权限数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"system:menu:view"})
    public List<Menu> list(Menu menu) {
        return menuService.findMenuByCondition(menu);
    }

    /**
     * 权限码唯一验证
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"system:menu:view"})
    public ValidResult checkUnique(Menu menu) {
        Menu m = menuService.findMenuByCode(menu.getCode());
        if (menu.getId() == null) {
            return ValidResult.result(m);
        }
        if (m == null || menu.getId().equals(m.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建权限
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"system:menu:edit"})
    public JsonResult save(Menu menu) {
        return isNotNull(menuService.saveMenu(menu));
    }

    /**
     * 修改权限
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"system:menu:edit"})
    public JsonResult update(Menu menu) {
        return isNotNull(menuService.updateMenu(menu));
    }

    /**
     * 删除权限
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"system:menu:edit"})
    public JsonResult del(Integer id) {
        return jsonResult(menuService.delMenu(id));
    }

}
